function timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        var D = date.getDate() + ' ';
        var h = date.getHours() + ':';
        var m = date.getMinutes() + ':';
        var s = date.getSeconds();
        return Y+M+D+h+m+s;
    }


layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;
		
	const tableVue=new Vue({
			el: '#authorList',
			data:{
				  all_data:null,
				  tableIns:null
			},
			methods:{
				tablefill:function(){
					this.tableIns =table.render({
							cellMinWidth : 95,
							page : true,
							height : "full-125",
							elem: '#authorList',
							id : "authorListTable",
							cols: [[ //标题栏
								{type: "checkbox", fixed:"left", width:50},
								{field: 'id', title: 'id', minWidth:20,width:50, align:"center"},
								{field: 'userId', title: '用户Id', minWidth:20,align:"center", width:80,templet:function(d){
												
												return d.userId==null?'外站':d.userId
								}},
								{field: 'penName', title: '笔名', minWidth:100, align:"center"},
								{field: 'state', title: '状态', align:'center',minWidth:50,templet:function(d){
										return d.state=='1'?'正常':'禁用'
								}},
								{field: 'startPenDate', title: '创建时间', minWidth:50, align:"center",templet:function(d){
											return timestampToTime(d.startPenDate)
											
								}},
							
								{title: '操作', minWidth:170, templet:'#authorListBar',fixed:"right",align:"center"}
							]],data:this.all_data
							
					});
				},
				getAuthor:function(type){
					var url=global_url+'/admin/author/authorAll'
					if(type!='1'&&type!=null){
						url+='/2'
					}else if(type=='1'){
						url+='/'+type
					}
					this.$http.get(url)
						.then(
						response=>{
							this.all_data=response.body.datas
							this.tablefill()
						},					
						response=>{
						})
				
				},
				//查看本站的作者
				getOwnAuthor:function(){
					this.$http.get(global_url+'/admin/author/authorOwnAll')
						.then(
						response=>{
							this.all_data=response.body.datas
							this.tablefill()
						},					
						response=>{
							layer.msg(response.body.status)
						})
				},
				//查看异站的作者
				getOutherAuthor:function(){
					this.$http.get(global_url+'/admin/author/authorOutherAll')
						.then(
						response=>{
							this.all_data=response.body.datas
							this.tablefill()
						},					
						response=>{
							layer.msg(response.body.status)
						})
				},
				//禁用
				prohibit:function(id){
					data={
						"id":id,
						"state":2
					}
					
					this.$http.post(global_url+'/admin/author/updata/',data)
						.then(
						response=>{
							layer.msg(response.body.datas)
							tableVue.getAuthor(1)

						},					
						response=>{
							layer.msg(response.body.status)
						})
				},
				//启用
				enable:function(id){
					data={
						"id":id,
						"state":1
					}
					this.$http.post(global_url+'/admin/author/updata/',data)
						.then(
						response=>{
							layer.msg(response.body.datas)
							tableVue.getAuthor(1)

						},					
						response=>{
							layer.msg(response.body.status)
						})
				},
				search:function(authorname){
					this.$http.get(global_url+'/admin/author/findName/'+authorname)
						.then(
						response=>{
							if(response.body.code=="-1"){
								layer.msg(response.body.datas)
								return
							}
							
							this.all_data=response.body.datas
							this.tablefill()
						},					
						response=>{
							layer.msg(response.body.status)
						})
				}
			}
		});
		
	tableVue.getAuthor(1)

  

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != ''){
					tableVue.search($(".searchVal").val())
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //编辑作者
    function editAuthor(edit){
			
			var index = layui.layer.open({
										title : "编辑作者",
										type : 2,
										content : "authorInfo.html",
										success : function(layero, index){
												var body = layui.layer.getChildFrame('body', index);
												if(edit){
												
														body.find(".id").val(edit.id); 
														body.find(".name").val(edit.penName); 
												}
										
												form.render();
			
												setTimeout(function(){
														layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
																tips: 3
														});
												},500)
										}
					})
				layui.layer.full(index);
				window.sessionStorage.setItem("index",index);
				//改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
				$(window).on("resize",function(){
						layui.layer.full(window.sessionStorage.getItem("index"));
				})
        
    }
	
    $(".addNews_btn").click(function(){
        editAuthor();
    })

    //批量删除
//     $(".delAll_btn").click(function(){
//         var checkStatus = table.checkStatus('userListTable'),
//             data = checkStatus.data,
//             newsId = [];
//         if(data.length > 0) {
//             for (var i in data) {
//                 newsId.push(data[i].newsId);
//             }
//             layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
//                 // $.get("删除文章接口",{
//                 //     newsId : newsId  //将需要删除的newsId作为参数传入
//                 // },function(data){
//                 tableVue.tableIns.reload();
//                 layer.close(index);
//                 // })
//             })
//         }else{
//             layer.msg("请选择需要删除的用户");
//         }
    // })

	//查询所有本站用户
	$(".normal_btn").click(function(){
		tableVue.getOwnAuthor()
	})
	
	//查询所有异站用户
	$(".outher_btn").click(function(){
		tableVue.getOutherAuthor()
	})
	
	//查询所有异常用户
	$(".extremely_btn").click(function(){
		tableVue.getAuthor("2")
	})
	
	
	
    //列表操作
    table.on('tool(authorList)', function(obj){
        var layEvent = obj.event,
        data = obj.data;
       if(layEvent === 'prohibit'){ //禁用
           
           if(data.state==2){
           	layer.msg('已经是禁用状态了')
           	return
           }
						layer.confirm("你确定要禁用该作者的所有权限吗？",{
            		icon: 3,
            		title:'系统提示',
            		cancel : function(index){
            				layer.close(index);
            		}
            },function(index){
								tableVue.prohibit(data.id)
            		layer.close(index);
            },function(index){
            		layer.close(index);
            });
        }else if(layEvent === 'enable'){ //启用
					
					if(data.state==1){
						layer.msg('已经是激活状态了')
						return
					}
				
				
					layer.confirm("你确定要激活该作者的所有权限吗？",{
							icon: 3,
							title:'系统提示',
							cancel : function(index){
									layer.close(index);
							}
					},function(index){
							tableVue.enable(data.id)
							layer.close(index);
					},function(index){
							layer.close(index);
					});
				}else if(layEvent== 'edit'){
					
					editAuthor(data)
				}
				
				
    });
	


})
